package com.ctcs.filter;


import com.ctcs.aspect.HttpAspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by duyaming on 2017/9/1.
 * 连接过滤filter，用于检验连接者是否有连接权限。如果没有，则将其踢掉。
 * 使用注解标注过滤器
 * @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
 * 属性filterName声明过滤器的名称,可选
 * 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
 */
@WebFilter(filterName="connectionAuthFilter",urlPatterns = "/*")
public class ConnectionAuthFilter implements Filter {

    private final static Logger logger = LoggerFactory.getLogger(ConnectionAuthFilter.class);
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
       logger.info("connectionAuthFilter");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        logger.info("doFilter");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        System.out.println(request);
        String path = request.getPathInfo();
        System.out.println(path);
        filterChain.doFilter(servletRequest, servletResponse);

    }

    @Override
    public void destroy() {
          logger.info("destroy");
    }
}
